import 'package:nylo_framework/nylo_framework.dart';
import 'package:flutter/material.dart';

class BlocProvider implements NyProvider {

  boot(Nylo nylo) async {

     return nylo;
  }

  afterBoot(Nylo nylo) async {


  }
}

abstract class BlocBase {
  Future getData({String labelId,int page});

  Future onRefresh(BuildContext context,{String labelId});

  Future onLoadMore({String labelId});

  void dispose();
}

class BlocsProvider<T extends BlocBase> extends StatefulWidget {
  BlocsProvider({
    Key? key,
    required this.child,
    this.bloc,
    this.userDispose = true,
  }) : super(key: key);

  final T? bloc;
  final Widget child;
  final bool userDispose;

  @override
  _BlocProviderState<T> createState() => _BlocProviderState<T>();

  static T? of<T extends BlocBase>(BuildContext context){
    // final type = _typeof<BlocsProvider<T>>();
    BlocsProvider<T>? provider = context.findAncestorWidgetOfExactType();
    return provider?.bloc;
  }

  static Type _typeof<T>() => T;
}

class _BlocProviderState<T> extends State<BlocsProvider<BlocBase>> {

  @override
  void dispose() {
    if(widget.userDispose) widget.bloc?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

